package com.example.mobileshop.utils;

import com.example.mobileshop.app.AppController;

import java.util.List;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.mobileshop.R;
import com.example.mobileshop.models.Shop;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
public class ShopListAdapter extends BaseAdapter {
 
	private List<Shop> listShops;
	private LayoutInflater layoutInflater;	
 
	public ShopListAdapter(Context context, List<Shop> listShops) {
		this.listShops = listShops;
		layoutInflater = LayoutInflater.from(context);
	}
 
	@Override
	public int getCount() {
		return listShops.size();
	}
 
	@Override
	public Object getItem(int position) {
		return listShops.get(position);
	}
 
	@Override
	public long getItemId(int position) {
		return position;
	}
 
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.list_view_row, null);
			holder = new ViewHolder();			
			holder.shopNameView = (TextView) convertView.findViewById(R.id.shop_name);			
			holder.imageView = (NetworkImageView) convertView.findViewById(R.id.shop_image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
 
		Shop newShop = listShops.get(position);
 		
		holder.shopNameView.setText(newShop.getShopName());		
 
		if (holder.imageView != null) {
			ImageLoader imageLoader = AppController.getInstance().getImageLoader();
			
			if(newShop.getShopIconUrl() == null){
				holder.imageView.setImageUrl(Const.URL_IC_SHOP_DEFAULT, imageLoader);				
			}else{
				holder.imageView.setImageUrl(newShop.getShopIconUrl(), imageLoader);						
			}												
		}
 
		return convertView;
	}
 
	static class ViewHolder {
		NetworkImageView imageView;
		TextView shopNameView;			
	}
}